package com.chance.cc.crawler.development.command.job.domain.cnhubei;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.dateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/3/11 18:00
 * @Description
 *      东湖社区
 **/
public class CnHubeiRealtimeCrawlerSchedulerJob {

    public static String domain = "cnhubei";

    private static final String metaServiceIp = "192.168.1.215";
    private static final int metaServicePort = 9599;

    private static final String kafkaTopciJob = "tmp_news";

    private static CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(metaServiceIp,metaServicePort);

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){
        CrawlerJob crawlerJob = crawlerSchedulejob();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("东湖社区每两个小时定时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerJob;

    }

    public static CrawlerJob crawlerSchedulejob(){
        String url = "http://bbs.cnhubei.com/search.php?mod=forum";
        String site = "searchKw";
        String siteBiz = "forum_realtime";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPageItem)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,siteBiz,"queue")))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null))
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        crawlerRequestRecord.setDownload(false);
        crawlerRequestRecord.setSkipPipeline(true);
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        Map<String,String> heads = new HashMap<>();
        heads.put("Content-Type","application/x-www-form-urlencoded");
        heads.put("Host","bbs.cnhubei.com");
        heads.put("Origin","http://bbs.cnhubei.com");
        heads.put("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36");
        heads.put("Cookie","haoe_af79_saltkey=CxAl2595; haoe_af79_lastvisit=1615351251; haoe_af79_pc_size_c=0; wdcid=6fedd44fca9c92f0; Hm_lvt_7c7368145650bd2ffeefcc96d2006f63=1615354863,1615431459; Hm_lvt_941d8bc973cf2e92916773f2b97fb56b=1615358144,1615434682; haoe_af79_sid=VUqI9P; Hm_lpvt_941d8bc973cf2e92916773f2b97fb56b=1615439920; wdlast=1615439921; wdses=2d814b87884ba544");
        heads.put("Referer","http://bbs.cnhubei.com/search.php?mod=forum");
        crawlerRequestRecord.getHttpRequest().setHeaders(heads);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(dateRange);
//        commentFilter.addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,"queue")));
        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null));
        crawlerRequestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("searchKwSourceUrl","http://bbs.cnhubei.com/search.php?mod=forum&searchid=%s&orderby=dateline&ascdesc=desc&searchsubmit=yes&kw=%s");
        Map<String,Object> map = new HashMap<>();
        map.put("searchsubmit","yes");
        map.put("srchtxt","%s");
        Map<String,Object> extras = new HashMap<>();
        extras.put("httpBody",map);
        crawlerRequestRecord.getHttpRequest().setExtras(extras);

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("cnhubei_forum_keyword",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domain+"/keys?site=forum")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(5)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,siteBiz,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain,site,siteBiz,"queue")))
//                .fileResultPipeline(null, "/data/chance_crawler_test/data/cnhubei/cnhubei_forum_realtime.log", false)
                .kafkaResultPipeline(null,kafkaTopciJob,null)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(keywordRecord)
                .build();
    }

}
